﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;

using HtmlExtractor;
using HtmlExtractor.Output;
using System.Xml.Linq;
using System.Linq;
using System.IO;
using Reader.Domain;
using System.Threading;
namespace Reader.View
{
    public class OnlineXamlView:UserControl
    {
        #region xaml
        protected virtual XDocument GenerateXaml(string html, string uri, string template)
        {
            throw new NotImplementedException();
        }
        public void LoadQidianXaml(string uri, string template)
        {
           // template = @"View/Chapter/qidianChapter.xml";
            if (!Uri.IsWellFormedUriString(uri,UriKind.Absolute))
                return;
            XDocument doc = null;
            string path = null;
            if (Cache.Current.TryGet(uri, out path))
            {
                doc = XDocument.Parse(File.ReadAllText(path));

                SetQidianElement(doc);
            }
            else if (ReaderConfig.IsUseLoaclBook && ReaderPath.TryGetSiteFileName(uri, out path))
            {
                doc = XDocument.Parse(File.ReadAllText(path));
                Cache.Current.Add(uri, path);
                SetQidianElement(doc);
            }
            else
            {
                WebClient client = new WebClient();
                client.Encoding = new Gb2312Encoding();
                client.DownloadStringAsync(new Uri(uri));
                client.DownloadStringCompleted += (s, args) =>
                {
                    if (args.Error == null)
                    {
                        doc = GenerateXaml(args.Result, uri, template);
                        path = Path.Combine(ReaderPath.CacheDirectory, ReaderPath.ConvertToWinPath(uri));    
                        File.WriteAllText(path, doc.ToString());
                        Cache.Current.Add(uri, path, ReaderConfig.MaxCacheTime);
                        SetQidianElement(doc);
                    }
                };
            }
        }
       
        public void LoadXaml(string uri,string template)
        {
            

            XDocument doc=null;
            string path = null;
            if (Cache.Current.TryGet(uri, out path))
            {
               doc =XDocument.Parse(File.ReadAllText(path));
               SetElement(doc);
               //LoadQidianXaml(uri, template);
            }
            else if (ReaderConfig.IsUseLoaclBook&&ReaderPath.TryGetSiteFileName(uri, out path))
            {
                doc = XDocument.Parse(File.ReadAllText(path));
                Cache.Current.Add(uri, path);
                SetElement(doc);
            }
            else
            {
                WebClient client = new WebClient();
                client.Encoding = new Gb2312Encoding();
                client.DownloadStringAsync(new Uri(uri));
                client.DownloadStringCompleted += (s, args) =>
                {
                    if (args.Error == null)
                    {
                        doc = GenerateXaml(args.Result, uri, template);
                       
                        path = Path.Combine(ReaderPath.CacheDirectory, ReaderPath.ConvertToWinPath(uri)); File.WriteAllText(path, doc.ToString());
                        if (BookChapter.IsEnd(doc))
                        {
                            Cache.Current.Add(uri, path, ReaderConfig.MinCacheTime);
                            BookChapter.SaveImage(doc, true, ReaderConfig.MinCacheTime);
                        }
                        else
                        {
                            Cache.Current.Add(uri, path, ReaderConfig.MaxCacheTime);
                            BookChapter.SaveImage(doc, true, ReaderConfig.MaxCacheTime);
                        }

                        SetElement(doc);
                    }
                };
            }
            
        }

        protected virtual void SetElement(XDocument rdoc)
        { throw new NotImplementedException(); }
        protected virtual void SetQidianElement(XDocument rdoc)
        { throw new NotImplementedException(); }
        #endregion

      
    }
}
